package com.zlp.filter;


import java.nio.charset.Charset;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * @author:ZouXiangHui
 * @Description:拦截器
 * @time:2018年9月18日 下午1:16:30
 */
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer{

    // 视图前缀
    private static final String VIEW_PREFIX = "/WEB-INF/views/";
    // 视图后缀
    private static final String VIEW_SUFFIX = ".jsp";
    // 视图的内容类型。
    private static final String VIEW_CONTENT_TYPE = "text/html;charset=UTF-8";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
    	registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    
    //配置 视图解析器
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setCache(false);
        resolver.setPrefix(VIEW_PREFIX);
        resolver.setSuffix(VIEW_SUFFIX);
        resolver.setExposeContextBeansAsAttributes(true);
        resolver.setContentType(VIEW_CONTENT_TYPE);
        return resolver;
    }
    
    /** ajax  UTF-8 */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    }

}